<?php

use Biz\model\Album\AlbumListModel;
use Biz\service\Album\AlbumListService;
use Doraemon\cache\AlbumCounter;

class GetAlbumByIdsAction extends ActionPlugin
{
    const USER_LEVEL = self::LEVEL_NOTHING;

    /**
     * @throws \Exception
     */
    public function main()
    {
        $albumIds = $this->getRequest()->getPost('album_ids', '');

        if (empty($albumIds)) {
            throw new Exception('album_ids参数缺失');
        }
        $albumIds = json_decode($albumIds);
        $result = [];
        if ($albumIds) {
            $albumIds = array_unique($albumIds);
            $albumList = AlbumListModel::selectByWhere([
                'album_id' => $albumIds,
                'status' => AlbumListModel::STATS_PUBLIC_SHOW_MAP,
                'ORDER' => ['album_id' => $albumIds],
            ]);
            $formatInfo = AlbumListService::formatAlbums($albumList);
            if ($formatInfo) {
                $albumCounters = AlbumCounter::getMulti($albumIds);
                foreach ($formatInfo as $val) {
                    $val['album_info']['joiner_num'] = $albumCounters[$val['album_info']['album_id']]['read'] ?: 0;
                    $result[] = $val;
                }
            }
        }
        $this->setSuccess($result, self::API_CODE_SUCCES);

    }
}
